package org.brisling.medical.queryService;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.DateUtil;
import org.brisling.medical.domain.AntiDrugDose;
import org.brisling.medical.service.AntiDrugDoseService;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <p>Title: QueryAntiDrugdoseService</p>
 * <p>Description: 抗菌药物使用率查询service</p>
 * <p>Company: tongking</p>
 * @author trastor
 * @since 1.0
 * @version 1.0
 * @date 2016年7月20日 下午10:00:47
 */
@RestController
@RequestMapping("/antiDrugDose/query")
public class QueryAntiDrugdoseService extends AbstractBaseQueryService<AntiDrugDose>{
	
	@Autowired
	private AntiDrugDoseService antiDrugDoseService;
	
	@Autowired
	private QueryDictionaryService qryDictionaryService;
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	
	public QueryAntiDrugdoseService(){
		this.moduleName = ConstSysParam.MODULE_ANTI_DRUG_DOSE;
		this.subSystem = ConstSysParam.SUBSYSTEM_MEDICAL;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.moduleName;
		this.clazz = AntiDrugDose.class;
		typeMap = this.getTypeMap(clazz);
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<AntiDrugDose> getListDomain(@RequestBody(required=false) String body){		
		
		Map<String,Object> map = this.getBodyParam(body);
		String userid = shiroSecurityService.getCurrentUserIdentify();
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_medical_all")){			
			addJobDeptConditionToMap(userid,map,"dept_code");			
			//查找科室数据		
		}
		//查询表
		return super.getListDomainByConditionAndOrder(null, map, typeMap, orderMap, true);
		
		
	}
	
	/**
	 * 获取部门统计指标
	 * @param body
	 * @return
	 */
	@RequestMapping("/getDeptListDomain.action")
	public ListDomain<AntiDrugDose> getDeptListDomain(@RequestBody(required=false) String body){		
		
		Map<String,Object> map = this.getBodyParam(body);
		@SuppressWarnings("unchecked")
		Map<String,String> paramMap = (Map<String, String>) map.get(ConstSysParam.CONDITION_MAP_KEY);
		String userid = shiroSecurityService.getCurrentUserIdentify();
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_medical_all")){
			String deptids = super.getJobDeptids(userid);
			paramMap.put("dept_code_in",deptids);			
			//查找科室数据		
		}
		paramMap.put("data_type", "2");
		map.put(ConstSysParam.CONDITION_MAP_KEY, paramMap);
		//查询表
		return super.getListDomainByConditionAndOrder(null, map, typeMap, orderMap, true);
		
	}
	
	
	/**
	 * 获取部门统计指标
	 * @param body
	 * @return
	 */
	
	private List<AntiDrugDose> getList(String monitor_year_month){
		
		String sql = String.format("select * from %1$s where monitor_year_month='%2$s'",this.tableName,monitor_year_month); 
		//查询表
		return super.getAllList(sql);
		
	}
	
	/**
	 * 查询实时数据
	 * @param body
	 * @return
	 */
	@RequestMapping("/getRealTimeListDomain.action")
	public ListDomain<AntiDrugDose> getRealTimeListDomain(@RequestBody(required=false) String body){		
		Map<String,Object> paramMap = this.getBodyParam(body);
		
		String[] monitorDate = super.getMonitorDate(paramMap);
		if(monitorDate!=null && monitorDate.length>1){			
			
			String start = monitorDate[0];
			String end = monitorDate[1];
			String year_month = monitorDate[2];
			List<AntiDrugDose> inlists =countMedicalInpatient (year_month,start,end,null);
			
			
			if(inlists!=null ){				
				antiDrugDoseService.save(inlists);
				return super.getListDomainByConditionAndOrderUseTable(body, typeMap, orderMap,true);
			}					
			
		}
		//查询表
		
		return new ListDomain<AntiDrugDose>();
	}
	
	/**
	 * 执行自动统计
	 */
	public void autoMonitor(){
		String[] yestodayMonth = DateUtil.getYestodayMonthDay();
		List<AntiDrugDose> inlists = countMedicalInpatient(yestodayMonth[2],yestodayMonth[0],yestodayMonth[1],null);
		
		
		//查询已有数据
		List<AntiDrugDose> oldLists = this.getList(yestodayMonth[2]);
		LinkedHashMap<Long,AntiDrugDose> lmap = new LinkedHashMap<Long,AntiDrugDose>();
		
		//将已有值存入临时map
		if(oldLists!=null && oldLists.size()>0){
			
			for(int i=0;i<oldLists.size();i++){
				AntiDrugDose b = oldLists.get(i);
				Long _key = null;
				if(b.getPatient_id()==null ){
					_key = b.getPatient_id();
				}
				lmap.put(_key, b);
			}
		}
		
		
		if(inlists!=null ){
			
			if(lmap!=null && lmap.size()>0){
				for(int i=0;i<inlists.size();i++){
					AntiDrugDose b = inlists.get(i);
					Long _key = null;
					if(b.getPatient_id()!=null ){
						_key = b.getPatient_id();
					}
					if(lmap.containsKey(_key)){
						inlists.get(i).setId(lmap.get(_key).getId());
					}
				}
			}
			
			antiDrugDoseService.save(inlists);			
		}	
	}
	/**
	 * 统计住院患者抗菌药物使用情况
	 * @param year_month
	 * @param startDate
	 * @param endDate
	 * @param countType	0,期间出院病人;1,期间在院病人
	 * @return
	 */
	public List<AntiDrugDose> countMedicalInpatient(String year_month,String startDate ,String endDate,String countType){
		
		
		//出院病人
		String _count_type = "0";
		
		//在院病人统计语句
		String sqlins= "anti_dose_drug_inpatient_in";
		
		//出院病人统计语句
		String sqlouts= "anti_dose_drug_inpatient_out";
		_count_type = countType==null?_count_type:countType;
		
		String sqlids = null;
		
		if(_count_type.compareTo("0")==0){			
			sqlids = sqlouts;
		}else{
			sqlids = sqlins;
		}
		//统计全院数据-住院
		
		
		
		String sql = qryDictionaryService.getSqlByBizcode(sqlids);
		List<AntiDrugDose> lists = new ArrayList<AntiDrugDose>();
		if(sql!=null){
			sql =String.format(sql, year_month,ConstSysParam.VIEW_INPATIENT_ITEM,startDate,endDate,ConstSysParam.MODULE_DRUG);
			//所有数据
			lists = super.getAllList(sql);
		}
				
		
		return lists;
		
	}
	
		
	
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public AntiDrugDose getOne(@RequestParam(value="id",required=false) Long id){
				
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public AntiDrugDose getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	
}
